package com.atguigu.delivery.servlet;

import com.atguigu.delivery.entity.Company;
import com.atguigu.delivery.entity.Delivery;
import com.atguigu.delivery.entity.SysUser;
import com.atguigu.delivery.service.api.CompanyService;
import com.atguigu.delivery.service.api.DeliveryService;
import com.atguigu.delivery.service.impl.CompanyServiceImpl;
import com.atguigu.delivery.service.impl.DeliveryServiceImpl;
import com.atguigu.delivery.util.DeliveryConstant;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet (urlPatterns = "/DeliveryServlet/ToEditPage")
public class DeliveryServletToEditPage extends HttpServlet {

    private DeliveryService deliveryService = new DeliveryServiceImpl();
    private CompanyService companyService = new CompanyServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 目标：调用 Service 方法把编辑页面所需数据查出来，带到编辑页面显示
        // 所需数据清单：Delivery 对象，List<Company>
        // 1、从请求参数中获取 Delivery 对象的 id 值
        String id = request.getParameter("id");

        // 2、根据 id 查询 Delivery 对象
        Delivery delivery = deliveryService.getDeliveryById(id);

        // 3、查询全部的 Company 组成的 List 集合
        List<Company> companyList = companyService.getAllCompanyList();

        // 4、把数据存入请求域
        request.setAttribute("delivery", delivery);
        request.setAttribute("companyList", companyList);

        // 5、转发到 edit.jsp
        request.getRequestDispatcher("/WEB-INF/pages/edit.jsp").forward(request, response);
    }
}
